/* ==========================================================================
 * Project Name: FELVServer
 * Package Features: Object Server Utils
 * Class Features: write log to File . File configure in file config.properties
 * File name: LogUtil.java
 * Summary: write log to File . File configure in file config.properties
 * Note:
 * History: New
 * ========================================================================== */
package tracking;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class LogUtil {
	private static final String LOG4J_APPENDER_FEVL_FILE_FILE = "log4j.appender.fevlFile.File";
	private static final String LOG4J_PATH_FILE = "/log4j.properties";
	private static Logger log4j = null;
	private static LogUtil instant = null;
	// flag show or unshow all Log System.out : true : show, false : unshow
	private static final boolean IS_DEBUG = true;
//	private static final boolean IS_DEBUG = false;
	
	

	private LogUtil(String name) {
		InputStream is = null;
		try {
			is = LogUtil.class.getClassLoader().getResourceAsStream(LOG4J_PATH_FILE);
			Properties p = new Properties();
			p.load(is);
			String config = p.getProperty(LOG4J_APPENDER_FEVL_FILE_FILE);
			File f = new File(config);
			String s3 = f.getParentFile().getPath(); 
			File f3 = new File(s3);
			if(!f3.isDirectory()){
				f3.mkdirs();
			}
			log4j = Logger.getLogger(name);
			PropertyConfigurator.configure(p);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}
	
	public static LogUtil getInstant(String name){
		if(instant == null){
			return new LogUtil(name);
		} else {
			return instant;
		}
	}
	
	public Logger getLogger(){
		return log4j;
	}
	
	/**
	 * log sysytem.out.print with show log, unshow log with flag
	 */
	
	public static void log (String TAG, String message) {
		log4j = getInstant("NECVN>>>").getLogger();
		
		if (IS_DEBUG) {
			log4j.debug("NECVN>>>" + TAG + ":" + message);
			System.out.println("NECVN>>>" + TAG + ":" + message);
		}
	}
	
	
}
